/**
 * @filename:SysRoleServiceImpl 2025-05-09
 * @project starhub-schema  V1.0
 */
package com.starhub.engine.sys.role.service.impl;

import com.starhub.common.bean.BrowserBean;
import com.starhub.common.exception.AppException;
import com.starhub.common.sys.user.LoginUser;
import com.starhub.engine.generator.genid.IdGenerator;
import com.starhub.engine.sys.role.entity.SysRole;
import com.starhub.engine.sys.role.dao.SysRoleDao;
import com.starhub.engine.sys.role.service.SysRoleService;
import com.starhub.utils.ColorUtil;
import com.starhub.utils.Util;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**   
 * @Description:(角色设置服务实现)
 *
 * @version: V1.0
 * @author: luosw
 * 
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleDao roleDao;

    /**
    * 新增
    * @param user
    * @param role
    * @return
    */
    public Long add(LoginUser user, SysRole role){

        SysRole record = new SysRole();
        record.setRoleCode(role.getRoleCode());
        List<SysRole> sysRoleList = roleDao.selectByPrimaryKeySelective(record);
        if(CollectionUtils.isNotEmpty(sysRoleList)){
            throw new AppException(500,"当前角色编码重复，请重新输入");
        }
        Long id = IdGenerator.generateId();
        role.setStatus(role.getStatus().equals("true") ? "active" : "inactive");
        //数据封装，默认值添加
        role.setId(id);
        role.setCreateTime(new Date());
        role.setUpdateTime(new Date());
        role.setDeleteType(0);

        roleDao.insertSelective(role);
        return id;
    }

    /**
    * 更新
    * @param user
    * @param role
    * @return
    */
    public SysRole update(LoginUser user, SysRole role){

        SysRole record = new SysRole();
        record.setRoleCode(role.getRoleCode());
        List<SysRole> sysRoleList = roleDao.selectByPrimaryKeySelective(record);
        if(CollectionUtils.isNotEmpty(sysRoleList)){
            List<Long> roleIds = sysRoleList.stream().map(SysRole::getId).collect(Collectors.toList());
            if(!(roleIds.size()==1 && roleIds.contains(user.getUser().getRoleId()))){
                throw new AppException(500,"当前角色编码重复，请重新输入");
            }
        }
        role.setUpdateTime(new Date());
        //不允许更新的属性设置为null
        role.setStatus(role.getStatus().equals("true") ? "active" : "inactive");
        roleDao.updateByPrimaryKeySelective(role);
        return role;
    }

    /**
    * 基本信息获取
    * @param user
    * @param id
    * @return
    */
    public SysRole getInfo(LoginUser user, Long id){

        SysRole role = roleDao.selectByPrimaryKey(id);
        return role;
    }

    /**
    * (批量)删除，同时支持单条或者批量,此方法为逻辑删除
    * @param user
    * @param ids
    */
    public void deleteByType(LoginUser user, String ids,int deleteType){

        //数组转换
        List<Long> idList = Arrays.stream(ids.split(","))
            .map((id) -> Long.parseLong(id))
            .collect(Collectors.toList());
            //删除状态，目前只有1的逻辑
        roleDao.deleteSysRoleByType(idList, deleteType);
    }

    /**
    * 根据id删除，此方法为真实删除
    * @param user
    * @param ids
    */
    public void deleteRecord(LoginUser user, String ids){

        //数组转换
        List<Long> idList = Arrays.stream(ids.split(","))
            .map((id) -> Long.parseLong(id))
            .collect(Collectors.toList());
        roleDao.deleteSysRoleRecord(idList);
    }

    /**
    * 列表查询
    * @param user
    * @param params
    */
    public List<SysRole> getList(LoginUser user,  Map<String , Object>  params){

        List<SysRole> roleList =  new ArrayList<SysRole>();
            //TODO
        int pageSize = 10;
        int current = 1;
        SysRole record = new SysRole();
        record.setDeleteType(0);
        String status = Util.null2String(params.get("status"));
        if(StringUtils.isNotEmpty(status)){
            record.setStatus(status);
        }
        roleList = roleDao.selectByPrimaryKeySelective(record);
        return roleList;
    }

    @Override
    public Map<String, BrowserBean> getActiveRole(LoginUser user, Map<String, Object> params) {

        Map<String, BrowserBean> result = new HashMap<>();
        SysRole record = new SysRole();
        record.setDeleteType(0);
        record.setStatus("active");
        List<SysRole> roleList = roleDao.selectByPrimaryKeySelective(record);
        // 查询所有有效权限
        List<String> dColors = ColorUtil.dColors;
        int i =0 ;
        if(CollectionUtils.isNotEmpty(roleList)){
            for(SysRole sysRole : roleList){
                int co = i % dColors.size();
                i++;
                String color = dColors.get(co);
                BrowserBean bean = new BrowserBean(Util.null2String(sysRole.getId()) ,sysRole.getRoleName(),color);
                result.put(Util.null2String(sysRole.getId()),bean);
            }
        }
        return result;
    }


}